package com.fs.ibox_h.dao.impl;

import com.fs.ibox_h.dao.BoticeDao;
import com.fs.ibox_h.pojo.Botice;
import com.fs.ibox_h.util.DateUtil;
import com.fs.ibox_h.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Author:HOU
 * date:2022/11/22  10:22
 * 案例:
 **/
public class BoticeDaoimpl implements BoticeDao {
    @Override
    public void add(Botice botice) {
        String sql="INSERT INTO tb_botice (b_type,b_time,title,content)  VALUES (?,?,?,?)";
        int i = JDBCUtil.executeUpdate(sql, botice.getB_type(), DateUtil.toString(new Date()), botice.getTitle(), botice.getContent());
    }

    @Override
    public List<Botice> queryAll(int currentPage,int pageSize) {
        StringBuffer  sql =new StringBuffer("SELECT * FROM tb_botice where 1=1 ");
        sql.append(" limit ? , ?");
        List<Botice> botices = JDBCUtil.executeQuery(sql.toString(), Botice.class, (currentPage-1)*pageSize, pageSize);
        return botices;
    }

    @Override
    public int count() {
        String sql="SELECT COUNT(1) FROM tb_botice";
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            psmt = conn.prepareStatement(sql);
            //给?赋值
            rs = psmt.executeQuery();
            rs.next();
            return rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(rs,psmt,conn);
        }
        return 0;

    }

    @Override
    public void delByid(int id) {
        String sql="DELETE FROM tb_botice where id=?";
        int i = JDBCUtil.executeUpdate(sql, id);

    }
}
